<?php

namespace app\purchase\logic;

use think\facade\Db;

class ArriveLogic extends BasicLogic
{

    /**
     * @desc   获取到货信息
     * @author luckyXiong
     * @date   2024-10-25
     */
    public function getArriveList($params=[]){
        $limit           = getIsset($params,'limit',10);
        $spu             = getIsset($params,'spu');
        $contract_number = getIsset($params,'contract_number');
        $store_time      = getIsset($params,'store_time');
        $where           = [];

        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }

        if (!empty($store_time)) {
            $where[]  = ['store_time', '>=', date('Y-m-d H:i:s',strtotime($store_time[0]))];
            $where[]  = ['store_time', '<=', date('Y-m-d H:i:s',strtotime($store_time[1]) + 3600*24-1)];
        }

        if($contract_number){
            $where[] = ['contract_number', '=',$contract_number];
        }

        $field = '*';
        $res   = Db::name('purchase_arrive_inventory')
            ->field($field)
            ->where($where)
            ->order('store_time desc')
            ->paginate($limit);
        $list = $res->items()?:[];

        foreach($list as $k=>$v){
            $list[$k]['attachment_images'] = $v['attachment_images'] ? json_decode($v['attachment_images'],true):'';
        }
        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }


    /**
     * @desc   获取次品信息
     * @author luckyXiong
     * @date   2024-10-27
     */
    public function getPantsList($params=[]){
        $limit           = getIsset($params,'limit',10);
        $spu             = getIsset($params,'spu');
        $contract_number = getIsset($params,'contract_number');
        $supply_id       = getIsset($params,'supply_id');
        $where           = [];

        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }


        if($supply_id){
            $where[] = ['supply_id', '=',$supply_id];
        }

        if($contract_number){
            $where[] = ['contract_number', '=',$contract_number];
        }


        $field = '*';
        $res   = Db::name('purchase_pants_inventory')
            ->field($field)
            ->where($where)
            ->order('check_time desc')
            ->paginate($limit);
        $list = $res->items()?:[];

        foreach($list as $k=>$v){
            $list[$k]['attachment_images'] = $v['attachment_images'] ? json_decode($v['attachment_images'],true):'';
        }
        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }


    /**
     * @desc   到货物流统计列表
     * @author luckyXiong
     * @date   2024-10-29
     */
    public function getList($params=[]){
        $limit           = getIsset($params,'limit',10);
        $spu             = getIsset($params,'spu');
        $purchase_code   = getIsset($params,'purchase_code');
        $where           = [];

        if($spu){
            $where[] = ['spu', 'like', '%' . $spu . '%'];
        }



        if($purchase_code){
            $where[] = ['purchase_code', '=',$purchase_code];
        }

        $field = '*';
        $res   = Db::name('purchase_count_inventory')
            ->field($field)
            ->where($where)
            ->order('create_time desc')
            ->paginate($limit);
        $list = $res->items()?:[];

        //批量获取图片地址;
        $spus         = array_column($list,'spu');
        $product_names= Db::name('product')->field('spu,product_name,product_images')->whereIn('spu',$spus)->select()->toArray();
        $products_name_map = array_column($product_names,null,'spu');
        foreach($list as $k=>$v){
            $product_images_product       = $products_name_map[$v['spu']]??[];
            $product_images               = $product_images_product['product_images']??[];
            $display_images               = $product_images ? current(json_decode($product_images)) : '';
            $list[$k]['product_images']   = $display_images;
            $defective_rate               = $v['defective_rate'] ?  ($v['defective_rate']*100).'%':'';
            $list[$k]['defective_rate']   = $defective_rate;
            $list[$k]['total_order_nums'] = $v['on_travel_nums']+$v['un_send_nums']+$v['inventory_nums'];
        }
        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }



    /**
     * @desc   获取次品信息
     * @author luckyXiong
     * @date   2024-10-29
     */
    public function get_defective_info($params=[]){
        $purchase_code      = getIsset($params,'purchase_code');
        $limit              = getIsset($params,'limit',10);
        $res   = Db::name('purchase_pants_inventory')
            ->field("*")
            ->where('purchase_code',$purchase_code)
            ->order('check_time desc')
            ->paginate($limit);
        $list = $res->items()?:[];

        foreach($list as $k=>$v){
            $list[$k]['attachment_images'] = $v['attachment_images'] ? json_decode($v['attachment_images'],true):'';
        }
        $data = [
            'list'   => $list,
            'total'  => $res->total(),
        ];
        return  $this->jsonMessage(200,true,'获取成功',$data);
    }





    /**
     * @desc   编辑次品信息
     * @author luckyXiong
     * @date   2024-10-29
     */
    public function edit_defective_info($params=[]){
        $id                = getIsset($params,'id');
        $purchase_code     = getIsset($params,'purchase_code');
        $contract_number   = getIsset($params,'contract_number');
        $spu               = getIsset($params,'spu');
        $check_nums        = getIsset($params,'check_nums');
        $unqualified_nums  = getIsset($params,'unqualified_nums');
        $defective_desc    = getIsset($params,'defective_desc');
        $attachment_images = '';
        if(isset($params['attachment_images'])&&$params['attachment_images']){
            $params['attachment_images'] = array_filter($params['attachment_images']);
            $attachment_images = json_encode(array_values($params['attachment_images']));
        }
        if(!$id) return  $this->jsonMessage(201,false,'ID不能为空!',null);
        $update = [
                  'purchase_code'     =>$purchase_code,
                  'contract_number'   =>$contract_number,
                  'spu'               =>$spu,
                  'check_nums'        =>$check_nums,
                  'unqualified_nums'  =>$unqualified_nums,
                  'defective_desc'    =>$defective_desc,
                  'update_time'       =>date("Y-m-d H:i:s", time()),
        ];
        if($attachment_images){
            $update['attachment_images'] = $attachment_images;
        }
        Db::name('purchase_pants_inventory')->where(['id'=>$id])->update($update);
        return  $this->jsonMessage(200,true,'操作成功',null);
    }









}